/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#ifndef FILE_UTIL_H
#define FILE_UTIL_H

#include "BioDefs.h"

#include <QtCore/QStringList>

namespace Bio {

class BIO_EXPORT FileUtil {
public:
	
	static QString	getFilename( const QString &filepath );
	static QString	getBaseFilename( const QString &filepath );
	static QString	getExtension( const QString &filepath );
	static QString	getPath( const QString &filepath );
	static QString	changeExtension( const QString &filepath, const QString &ext );

	static bool	expandGZFile( const QString &filename, const QString &target = QString::null );
	static bool	compressGZFile( const QString &filename, const QString &target = QString::null );

	static bool	extractTarFile( const QString &filename, const QString &path = QString::null, bool expandGZ = false );
	static bool	createTarFile( const QString &path, const QString &target = QString::null, bool compressGZ = false );
	static bool	createTarFile( const QStringList &files, const QString &target, bool compressGZ = false );

	static bool	extractTarFile( QIODevice *input, const QString &path );
	static bool	createTarFile( const QString &path, QIODevice *output );
	static bool	createTarFile( const QStringList &files, QIODevice *output );

	static QStringList	getAllFiles( const QString &path );

private:

	static bool	extractFileFromTar( QIODevice *input, const QString &path );
	static bool	addFileToTar( const QString &filename, QIODevice *dev );

	static QString	readTarString( char *&buff, int fieldLength );
	static int		readTarInt( char *&buff, int fieldLength );

	static void	writeTarString( const QString &str, char *&buf, int fieldLength );
	static void	writeTarInt( int val, char *&buf, int fieldLength );
};

}; // Bio

#endif // FILE_UTIL_H